﻿namespace LaoLuo
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 提供TaskExecutor&lt;T>的快捷调用
    /// </summary>
    public static class TaskExecutor
    {
        /// <summary>
        /// 快捷调用类实例
        /// </summary>
        private static TaskExecutorShortcut shortcut = new TaskExecutorShortcut();

        /// <summary>
        /// TaskExecutor&lt;T>的快捷调用
        /// </summary>
        public static TaskExecutorShortcut Shortcut 
        { 
            get
            {
                return shortcut;
            }
        }

        /// <summary>
        /// 提供TaskExecutor&lt;T>的快捷调用
        /// </summary>
        public class TaskExecutorShortcut
        {
            /// <summary>
            /// 创建并执行TaskExecutor&lt;T>的Handle方法,完成后会释放资源
            /// </summary>
            /// <typeparam name="T">数据类型</typeparam>
            /// <param name="data">数据</param>
            /// <param name="handleMethod">数据处理方法</param>
            /// <param name="handleTimesLimit">最大处理次数</param>
            /// <param name="waitBeforeRetry">重试前等待秒数</param>
            /// <returns>处理失败的数据</returns>
            public IEnumerable<T> HandleNew<T>(IEnumerable<T> data, Func<T, bool> handleMethod, int handleTimesLimit = 3, int waitBeforeRetry = 0)
            {
                using (var te = new TaskExecutor<T>(data, handleMethod, handleTimesLimit, waitBeforeRetry))
                {
                    return te.Handle();
                }
            }

            /// <summary>
            /// 创建并执行TaskExecutor&lt;T>的Handle方法,完成后会释放资源
            /// </summary>
            /// <typeparam name="T">数据类型</typeparam>
            /// <param name="data">数据</param>
            /// <param name="handleMethod">数据处理方法</param>
            /// <param name="handleTimesLimit">最大处理次数</param>
            /// <param name="waitBeforeRetry">重试前等待秒数</param>
            /// <returns>处理失败的数据</returns>
            public IEnumerable<T> HandleNew<T>(T data, Func<T, bool> handleMethod, int handleTimesLimit = 3, int waitBeforeRetry = 0)
            {
                using (var te = new TaskExecutor<T>(data, handleMethod, handleTimesLimit, waitBeforeRetry))
                {
                    return te.Handle();
                }
            }
        }
    }
}
